package service;

import domain.Account;
import persistence.AccountDAO;
import persistence.impl.AccountDAOImpl;

public class AccountService {
    private AccountDAO accountDAO;
    private String info;

    public AccountService(){
        this.accountDAO = new AccountDAOImpl();
    }

    public Account confirmUserByIdAndPassword(String userid, String password){
        return this.accountDAO.findUserByIdAndPassword(userid,password);
    }

    public boolean newUser(Account user){
        if(accountDAO.findUserById(user.getUserid())){
            this.info = "该用户名已被注册";
            return false;
        }
        if(!accountDAO.addUser(user)){
            this.info = "注册失败";
            return false;
        }
        return true;
    }

    public boolean updateUser(String newpassword, Account user){
        return (newpassword == null ? accountDAO.changeInfo(user) : (accountDAO.changePassword(newpassword,user.getUserid()) && accountDAO.changeInfo(user)));
    }

    public boolean changeInfo(Account account){
        return accountDAO.changeInfo(account);
    }

    public boolean changePassword(String password, String userid){
        return accountDAO.changePassword(password,userid);
    }

    public String getInfo() {
        return info;
    }

    public Account findUserByIdAndPhone(String userid, String phone){
        return accountDAO.findUserByIdAndPhone(userid, phone);
    }

    public boolean findUserById(String userid){
        return accountDAO.findUserById(userid);
    }
}
